package com.ts.opengl.simpleDraw

import android.content.Context
import android.opengl.GLES20.GL_COLOR_BUFFER_BIT
import android.opengl.GLES20.GL_DEPTH_BUFFER_BIT
import android.opengl.GLES20.GL_FLOAT
import android.opengl.GLES20.GL_NONE
import android.opengl.GLES20.GL_STENCIL_BUFFER_BIT
import android.opengl.GLES20.GL_TRIANGLES
import android.opengl.GLES20.glClear
import android.opengl.GLES20.glClearColor
import android.opengl.GLES20.glDrawArrays
import android.opengl.GLES20.glEnableVertexAttribArray
import android.opengl.GLES20.glGenBuffers
import android.opengl.GLES20.glUseProgram
import android.opengl.GLES20.glVertexAttribPointer
import android.opengl.GLES30
import android.opengl.GLSurfaceView
import android.util.Log
import com.ts.audiocamera.R
import com.ts.opengl.simpleDraw.openglUtil.Companion
import java.nio.ByteBuffer
import java.nio.ByteOrder
import java.nio.FloatBuffer
import javax.microedition.khronos.egl.EGLConfig
import javax.microedition.khronos.opengles.GL10

class trigleRender(val context: Context):GLSurfaceView.Renderer {


    var VERTEX: FloatArray = floatArrayOf(
        -0.5f, -0.5f, 0.0f,
        0.5f, -0.5f, 0.0f,
        0.0f,  0.5f, 0.0f,

    )

    var COLOR: FloatArray = floatArrayOf(
          0.6f, 0.6f, 0.6f,1.0f,
          0.7f, 0.6f, 0.6f,1.0f,
          0.6f, 0.6f, 0.6f,1.0f
//         0.5f, 0.5f, 0.3f,1.0f,
//          0.1f, 0.5f, 0.2f,1.0f

        )
    private  val TAG = "trigleRender"

    var vertexBuffer: FloatBuffer?=null
    var vertexColor: FloatBuffer?=null
    private var program=0;
    override fun onSurfaceCreated(gl: GL10?, config: EGLConfig?) {
        Log.e(TAG,"onSurfaceCreated")

        program=openglUtil.createProgram(context, R.raw.tri_ver,R.raw.tri_frg);
//
        //        建立通道
        vertexBuffer =
            ByteBuffer.allocateDirect(VERTEX.size * 4)
                .order(ByteOrder.nativeOrder())
                .asFloatBuffer()
        vertexBuffer?.put(VERTEX)
        vertexBuffer?.position(0)

        vertexColor=
            ByteBuffer.allocateDirect(COLOR.size * 4)
                .order(ByteOrder.nativeOrder())
                .asFloatBuffer()
        vertexColor?.put(COLOR)
        vertexColor?.position(0)

    }

    override fun onSurfaceChanged(gl: GL10?, width: Int, height: Int) {
        Log.e(TAG,"onSurfaceChanged")
        GLES30.glViewport(0, 0, width, height)
    }

    override fun onDrawFrame(gl: GL10?) {
        Log.e(TAG,"onDrawFrame")
        glClear(GL_STENCIL_BUFFER_BIT or GL_COLOR_BUFFER_BIT
                or GL_DEPTH_BUFFER_BIT)

        glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
        glUseProgram(program)


        vertexBuffer?.position(0)
//        glGenBuffers(BU)
//        glVertexAttribPointer
       glVertexAttribPointer (0, 3, GL_FLOAT,
            false, 0, vertexBuffer );
        GLES30.glEnableVertexAttribArray(0);  //

        vertexColor?.position(0)
        glVertexAttribPointer (1, 4, GL_FLOAT,
                false, 0, vertexColor
            )
        GLES30.glEnableVertexAttribArray(1);
//        glEnableVertexAttribArray (0);
        glDrawArrays (GL_TRIANGLES, 0, 3);
        glUseProgram (GL_NONE);
    }



}